<template>
  <div class="con-cah">
    <chart :options="pie" :init-options="initOptions" ref="pie" autoresize />
  </div>
</template>
<script>
import ECharts from "@/components/echarts/ECharts.vue";

export default {
  components: {
    chart: ECharts
  },
  props: {
    //[
    //{name:"推荐量", color:"#34243",value:123},
    //{name:"阅读量", color:"#34242",value:123},
    //{name:"转发量", color:"#34243",value:123},
    //{name:"收藏量", color:"#34243",value:123}
    //]
    data: {
      type: Array,
      default: []
    },
    title: {
      type: String,
      default: ""
    }
  },
  watch: {
    data(newVal, oldVue) {
      this.init();
    }
  },
  data() {
    return {
      initOptions: {
        renderer: "canvas"
      },
      pie: {
        title: {
          text: this.title,
          x: "center",
          textStyle: {  //文本颜色
            color: "#71e3fe",
            fontSize: 16
          },
          top: 10
        },
        // tooltip: {     //tooltip 鼠标移入悬浮显示的内容
        //   show: true,
        //   trigger: "item",
        //   formatter: "{a} <br/>{b} : {c} ({d}%)"
        // },
         legend: {
          orient: "vertical",
          right: 0,
          textStyle: {
            color: '#fff'
          },
          
          data: [] 
        },
        series: [
          {
            name: this.title,
            type: "pie", //环形图的type和饼图相同
            radius: ["50%", "80%"], //饼图的半径，第一个为内半径，第二个为外半径
            center: ["50%", "50%"],
            backgroundColor: null,
            color: ["#00E9FE", "#d4effa"],
            avoidLabelOverlap: false,
            label: {
              //正常时候的样式
              show: false,
              position: "center",
              textStyle: {
                fontSize: 20,
                fontWeight: "bold"
              },
              formatter: "{b}\n{d}%" //环形图内部显示内容 b代表标题， d代表百分比内容
            },
            emphasis: {
              //选中时候的样式
              label: {
                show: true,
                fontSize: "12",
                fontWeight: "bold",
                color: "#fff"
              }
            },
            data: [],
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      }
    };
  },
  methods: {
    init() {
      var list = [];
      for (var item of this.data) {
         this.pie.legend.data.push(item.name)
        list.push({
          value: item.value,
          name: item.name,
          radius: item.radius,
          itemStyle: {
            normal: {
              color: item.color
            }
          }
        });
      }
      this.pie.series[0].data = list;
    }
  },
  mounted() {
    this.init();
  }
};
</script>
<style  scoped>
.con-cah {
  width: 100%;
  height: 100%;
}
</style>